// pages/iPhone/iPhone.js
var Session = require('../../vendor/qcloud-weapp-client-sdk/lib/session.js');
var config = require('../../config.js');
var commons = require('../../commons.js');
var qcloud = require('../../vendor/qcloud-weapp-client-sdk/index');
var register = require('../../template/register/register.js');
var utils = require('../../vendor/qcloud-weapp-client-sdk/lib/utils.js');
var index = {

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: "",
    sVerCodeData: '',
    inputName: "",
    time: 120,
    CodeID: '',
    yzmDisabled: false, //验证码初始状态
    sureDisabled: false //确定手机号初始状态
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  /**
   * 获取验证码验证手机号
   */
  _checkPhone: function(mobile) {

  },
  //输入手机号
  bindKeyInput: function(e) {
    this.setData({
      inputValue: e.detail.value
    })
    this.btnTrue();
  },
  //输入验证码
  sVerCodeNum: function(e) {
    this.setData({
      sVerCodeData: e.detail.value
    })
    this.btnTrue();
  },
  //判断按钮状态
  btnTrue: function() {
    if (this.data.sVerCodeData.length >= 4 && this.data.inputValue.length > 0) {
      this.setData({
        sureDisabled: true
      })
    } else {
      this.setData({
        sureDisabled: false
      })
    }
  },
  // 姓名
  bindNameInput: function(e) {
    this.setData({
      inputName: e.detail.value
    })
  },
  // 获取验证码
  toBindTelUrl: function() {
    var that = this;
    var PhoneNum = this.data.inputValue;
    var Name = this.data.inputName;
    var mobileReg = /^[1][3,4,5,7,8][0-9]{9}$/;
    var t = 120;
    if (Name == '') {
      utils.showError('请输入姓名')
      return false;
    } else if (PhoneNum == '') {
      utils.showError('请输入手机号')
      return false;
    } else if (!mobileReg.test(PhoneNum)) {
      utils.showError('请输入正确手机号')
      return false;
    } else {
      that.iPhoneYanZ(PhoneNum);
      that.setData({
        yzmDisabled: true
      })
      //倒计时发送短信
      var timeNum = setInterval(function() {
        t--;
        if (t > 0) {
          that.setData({
            time: t
          });
        } else {
          clearInterval(timeNum);
          that.setData({
            yzmDisabled: false,
            time: 60
          })

        }
      }, 1000)
    }
  },
  //*********
  //执行发送验证码
  //******
  iPhoneYanZ: function(num) {
    var that = this;
    qcloud.request({
      login: false,
      url: config.service.sendSmsUrl,
      data: {
        mobile: num,
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        if (res.data.code == 0) {
          utils.showError(res.data.msg);
          that.setData({
            CodeID: res.data.data
          })
        } else {
          utils.showError('发送失败请重试')
        }
      },
      fail: function(res) {
        utils.showError(res)
      },
      complete: function() {
        setTimeout(function() {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });
  },
  //*********
  //提交验证码
  //******
  clickTiJiaoYzm: function() {
    var that = this;
    qcloud.request({
      login: false,
      url: config.service.toBindTelUrl,
      data: {
        sVerCode: that.data.sVerCodeData, //验证码
        sMobile: that.data.inputValue, //手机号
        sUserName: that.data.inputName, //姓名
        CodeID: that.data.CodeID //CodeID
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function(res) {
        if (res.data.code == 0) {
          utils.showError(res.data.msg);
        } else {
          utils.showError('发送失败请重试')
        }
      },
      fail: function(res) {
        utils.showError(res.msg)
      },
      complete: function() {
        setTimeout(function() {
          wx.hideLoading();
          wx.stopPullDownRefresh();
        }, 500)
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

};
var data = Object.assign(index.data, register.data);
var page = Object.assign(index, register);
page.data = data;
Page(page);